package com.all.play.myapplication.adapter;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.all.play.myapplication.Bean.NewsResaultBeanData;
import com.all.play.myapplication.R;
import com.all.play.myapplication.adapter.inneradapter.MyActAdapter;
import com.all.play.myapplication.adapter.inneradapter.MyChannelAdapter;
import com.all.play.myapplication.adapter.inneradapter.MySeckillAdapter;
import com.all.play.myapplication.utils.Constants;
import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;
import com.youth.banner.transformer.RotateDownTransformer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 聪明一只哈 on 2018/4/20.
 * Time: 2018/4/20  11:25
 */

public class MyNewsAdapter extends RecyclerView.Adapter {


    //广告类型
    public static final int BANNER = 0;
    //频道类型
    public static final int CHANNEL = 1;
    //活动类型
    public static final int ACT = 2;
    //秒杀类型
    public static final int SECKILL = 3;
    //推荐类型
    public static final int RECOMMEND = 4;
    //热卖类型
    public static final int HOT = 5;
    private static final String GOODSINFO = "goodsinfo";

    //定义当前类型
    private int currentType = BANNER;

    private Context mContext;
    private NewsResaultBeanData beanData;


    public MyNewsAdapter(Context mContext, NewsResaultBeanData beanData) {

        this.mContext = mContext;
        this.beanData = beanData;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == BANNER) {
            return new MyItemOneHolder(View.inflate(mContext, R.layout.banner_viewpage, null));
        } else if (viewType == CHANNEL) {
            return new MyItemTwoHolder(View.inflate(mContext, R.layout.channel_item, null));
        } else if (viewType == ACT) {
            return new MyItemThreeHolder(View.inflate(mContext, R.layout.act_item, null));
        } else if (viewType == SECKILL) {
            return new MyItemFourthHolder(View.inflate(mContext, R.layout.seckill_item, null));

        } else if (viewType == RECOMMEND) {

        } else if (viewType == HOT) {

        }


        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (getItemViewType(position) == BANNER) {
            List<NewsResaultBeanData.ResultBean.BannerInfoBean> banner_info = beanData.getResult().getBanner_info();
            if (banner_info != null && banner_info.size() > 0) {
                MyItemOneHolder holderone = (MyItemOneHolder) holder;
                holderone.setdata(banner_info);
            }
        } else if (getItemViewType(position) == CHANNEL) {
            List<NewsResaultBeanData.ResultBean.ChannelInfoBean> channel_info = beanData.getResult().getChannel_info();
            if (channel_info != null && channel_info.size() > 0) {
                MyItemTwoHolder holdertwo = (MyItemTwoHolder) holder;
                holdertwo.setdata(channel_info);
            }
        } else if (getItemViewType(position) == ACT) {
            List<NewsResaultBeanData.ResultBean.ActInfoBean> act_info = beanData.getResult().getAct_info();
            if (act_info != null&&act_info.size()>0) {
                MyItemThreeHolder holderthree = (MyItemThreeHolder) holder;
                holderthree.setdata(act_info);
            }
        }else if(getItemViewType(position)==SECKILL){
            List<NewsResaultBeanData.ResultBean.SeckillInfoBean.ListBean> beanList = beanData.getResult().getSeckill_info().getList();
            if(beanList!=null&&beanList.size()>0){
                MyItemFourthHolder holderfouth= (MyItemFourthHolder) holder;
                holderfouth.setdata(beanList);
            }
        }


    }

    @Override
    public int getItemViewType(int position) {

        switch (position) {
            case BANNER:
                currentType = BANNER;
                break;
            case CHANNEL:
                currentType = CHANNEL;
                break;
            case ACT:
                currentType = ACT;
                break;
            case SECKILL:
                currentType = SECKILL;
                break;
            case RECOMMEND:
                currentType = RECOMMEND;
                break;

            case HOT:
                currentType = HOT;
                break;

            default:

                break;
        }


        return currentType;
    }

    @Override
    public int getItemCount() {
        return 4;
    }

    //条目一
    class MyItemOneHolder extends RecyclerView.ViewHolder {

        Banner banner;

        public MyItemOneHolder(View itemView) {
            super(itemView);
            banner = (Banner) itemView.findViewById(R.id.banner);

        }

        public void setdata(List<NewsResaultBeanData.ResultBean.BannerInfoBean> banner_info) {
            //设置banner数据
            List<String> imageurl = new ArrayList<>();
            for (int i = 0; i < banner_info.size(); i++) {
                String s = Constants.BASE_URl_IMAGE + banner_info.get(i).getImage();
                Log.i("HomeResaultB", "setData: " + s);
                imageurl.add(Constants.BASE_URl_IMAGE + banner_info.get(i).getImage());
            }
            //必须要有,不然图片出不来
            banner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    Glide.with(mContext).load(path).into(imageView);
                }
            });

            //设置循环指示
            banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);

            //设置手风琴效果
            banner.setBannerAnimation(Transformer.Accordion);
            banner.setImages(imageurl);
            banner.start();

            //设置点击
            banner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(int position) {
                    //poisition需要-1
                    //startGoodsActivity(hot_info);

                }
            });

        }
    }

    //条目二
    class MyItemTwoHolder extends RecyclerView.ViewHolder {

        GridView gridview;

        public MyItemTwoHolder(View itemView) {
            super(itemView);

            gridview = (GridView) itemView.findViewById(R.id.gv_channel);

        }


        public void setdata(List<NewsResaultBeanData.ResultBean.ChannelInfoBean> channel_info) {
            MyChannelAdapter myChannelAdapter = new MyChannelAdapter(mContext, channel_info);
            gridview.setAdapter(myChannelAdapter);

            gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                    Toast.makeText(mContext, ""+position, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    //条目三
    class MyItemThreeHolder extends RecyclerView.ViewHolder {

        ViewPager act_viewpager;

        public MyItemThreeHolder(View itemView) {
            super(itemView);
            act_viewpager = (ViewPager) itemView.findViewById(R.id.act_viewpager);
        }

        public void setdata(List<NewsResaultBeanData.ResultBean.ActInfoBean> act_info) {
            Log.i("NewsResaultBe", "setdata: " + act_info.size());
            //设置间距
            act_viewpager.setPageMargin(20);
            act_viewpager.setOffscreenPageLimit(3);
            MyActAdapter myActAdapter = new MyActAdapter(mContext, act_info);
            act_viewpager.setAdapter(myActAdapter);

            act_viewpager.setPageTransformer(true, new RotateDownTransformer());
        }
    }

    //条目四
    class MyItemFourthHolder extends RecyclerView.ViewHolder {

        RecyclerView rv_seckill;
        public MyItemFourthHolder(View itemView) {
            super(itemView);
            rv_seckill= (RecyclerView) itemView.findViewById(R.id.rv_seckill);
        }

        public void setdata(List<NewsResaultBeanData.ResultBean.SeckillInfoBean.ListBean> seckill_info) {

            MySeckillAdapter mySeckillAdapter = new MySeckillAdapter(mContext,seckill_info);

            rv_seckill.setAdapter(mySeckillAdapter);

            /*
            * //new Stagger布局管理器,布局管理器所需参数:1.规定显示几列 2.item排列方向
                StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(2,
                orientation ? StaggeredGridLayoutManager.VERTICAL :
                StaggeredGridLayoutManager.HORIZONTAL);

                //通过布局管理器控制条目排列的顺序  true:反向显示 false:正向显示
                staggeredGridLayoutManager.setReverseLayout(inversion);
                //设置布局管理器，参数StaggeredGridLayoutManager，可以是RecyclerView实现和StaggeredGridView一样的效果
                recycleview.setLayoutManager(staggeredGridLayoutManager);
            * */

            StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
            layoutManager.setReverseLayout(true);//倒叙排列
            rv_seckill.setLayoutManager(layoutManager);


            mySeckillAdapter.setGetLocationListener(new MySeckillAdapter.GetLocationListener() {
                @Override
                public void showlocate(int position) {
                    Toast.makeText(mContext, "点击的位置是"+position, Toast.LENGTH_SHORT).show();
                }
            });


        }
    }


}
